package metier;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Entreprise implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3089862368237709099L;	

	private int idEntreprise = -1;
	private String login;
	private String mdp;
	private Set<Stage> listeStages = new HashSet<Stage>();
	
	private void testerExceptionLogin(String login)
			throws Exception {
		if ((login==null) || login.equals(""))
			throw new Exception("Login ["+login+"] incorrect");
	}

	private void testerExceptionMdp(String mdp)
			throws Exception {
		if ((mdp==null) || mdp.equals(""))
			throw new Exception("Mot de passe ["+mdp+"] incorrect");
	}

	public Entreprise(int idEntreprise, String login, String mdp) throws Exception {
		
		super();
		
		testerExceptionLogin(login);
		testerExceptionMdp(mdp);
		
		this.idEntreprise = idEntreprise;	
		this.login = login;
		this.mdp = mdp;
		
	}
	
	public Entreprise(String login, String mdp) throws Exception {
			
		testerExceptionLogin(login);
		testerExceptionMdp(mdp);
		
		this.login = login;
		this.mdp = mdp;
		
	}

	/**
	 * Bien qu'illogique avec le mod�le UML, ce constructeur est obligatoire pour respecter la convention
	 * des JavaBean utilisŽe par certains frameworks (Struts, Hibernate, ...)
	 */
	public Entreprise()
	{		
		this.login = "Neant";
		this.mdp = "Neant";
	}

	public int getIdEntreprise() {
		return idEntreprise;
	}

	public void setIdEntreprise(int idEntreprise) {
		this.idEntreprise = idEntreprise;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getMdp() {
		return mdp;
	}

	public void setMdp(String mdp) {
		this.mdp = mdp;
	}

	public Set<Stage> getListeStages() {
		return listeStages;
	}

	public void setListeStages(Set<Stage> listeStages) {
		this.listeStages = listeStages;
	}
	
	public void addStage(Stage stage) {
		this.listeStages.add(stage);
	}
	
	public int nbStages () {
		return this.listeStages.size();
	}

	@Override
	public String toString() {
		return "Entreprise [idEntreprise=" + idEntreprise + ", login=" + login
				+ ", mdp=" + mdp + ", listeStages=" + listeStages + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + idEntreprise;
		//result = prime * result
				//+ ((listeStages == null) ? 0 : listeStages.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((mdp == null) ? 0 : mdp.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Entreprise other = (Entreprise) obj;
		if (idEntreprise != other.idEntreprise)
			return false;
		
		// modification de la fonction equals car appel rŽcursif croisŽ! 
		
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (mdp == null) {
			if (other.mdp != null)
				return false;
		} else if (!mdp.equals(other.mdp))
			return false;
		return true;
	}
	
	
}
